package com.navinfo.collect.library.data.flutter.datahandler

import android.content.Context
import android.util.Log
import com.navinfo.collect.library.data.DataConversion
import com.navinfo.collect.library.data.dao.impl.MapLifeDataBase
import com.navinfo.collect.library.data.entity.CustomLayerItem
import com.navinfo.collect.library.data.entity.LayerManager
import com.navinfo.collect.library.data.datahandler.DataLayerHandler
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import org.json.JSONArray

/**
 * 数据图层管理
 */
class FlutterDataLayerHandler(context: Context, dataBase: MapLifeDataBase) :
    DataLayerHandler(context, dataBase) {

    fun createDataLayerTable(call: MethodCall, result: MethodChannel.Result) {
        try {
            if (call.arguments is Map<*, *>) {
//                "style": style,
                val layer = LayerManager();
                layer.id = call.argument("uuid")
                layer.projectId = call.argument("projectId")
                layer.geometryType = call.argument<Int>("geometryType")!!
                layer.layerName = call.argument("layerName")
                layer.importTime = call.argument("import_time")
                layer.describe = call.argument("describe")
                layer.visibility = call.argument<Boolean>("visibility") == false
                if (call.hasArgument("layerItems")) {
                    val itemList = call.argument<List<Map<*, *>>>("layerItems")
                    val list = DataConversion.listMapToCustomLayerItemsList(itemList!!)
                    createTable(layer, list) { res, _ ->
                        result.success(res)
                    };
                } else {
                    result.success(false)
                }
            } else {
                result.success(false)
            }
        } catch (e: Throwable) {
            Log.e("jingo", e.message);
            Log.e("jingo", e.stackTraceToString());
            Log.e("jingo", e.toString());
            result.success(false)
        }
    }

    /**
     * 获取全部数据图层信息
     */
    fun getDataLayerList(call: MethodCall, result: MethodChannel.Result) {
        getDataLayerList { list ->
            val newList = mutableListOf<Map<*, *>>()
            for (item in list) {
                var itemList = DataConversion.jsonToLayerItemsMap(item.bundle)
                val map = mapOf(
                    "uuid" to item.id,
                    "projectId" to item.projectId,
                    "geometryType" to item.geometryType,
                    "layerName" to item.layerName,
                    "import_time" to item.importTime,
                    "describe" to item.describe,
                    "visibility" to item.visibility,
                    "layerItems" to itemList
                )
                newList.add(map)
            }
            result.success(newList)
        };
    }

    /**
     * 获取某个数据图层
     */
    fun getDataLayer(call: MethodCall, result: MethodChannel.Result) {
        if (call.arguments is String) {
            getDataLayer(call.arguments as String) { layer ->
                var itemList = DataConversion.jsonToLayerItemsMap(layer.bundle)
                result.success(
                    mapOf(
                        "uuid" to layer.id,
                        "projectId" to layer.projectId,
                        "geometryType" to layer.geometryType,
                        "layerName" to layer.layerName,
                        "import_time" to layer.importTime,
                        "describe" to layer.describe,
                        "visibility" to layer.visibility,
                        "layerItems" to itemList
                    )
                )
            }
        } else {
            result.success("false")
        }

    }
}